package com.hyw.supply.exception;

import com.hyw.supply.model.vo.CommonResult;
import com.hyw.supply.enums.ResultCodeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author 曾智平
 * @Description: 异常处理
 * @date 2022-07-14
 */
@ControllerAdvice
public class ExceptionHandles {

    private Logger logger = LoggerFactory.getLogger(ExceptionHandles.class);

    /**
     * 基础异常
     *
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = BaseException.class)
    public CommonResult baseExceptionHandle(BaseException e) {
        logger.error("error :{}", e.getMessage());
        return CommonResult.error(e.getMessage());
    }

    /**
     * 全局异常
     *
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public CommonResult exceptionHandle(Exception e) {
        logger.error("error :{}", e.getMessage());
        return CommonResult.result(ResultCodeEnum.ERROR);
    }

    /**
     * 认证异常
     *
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = BadCredentialsException.class)
    public CommonResult badCredentialsException(BadCredentialsException e) {
        logger.error("error :{}", e.getMessage());
        return CommonResult.error(e.getMessage());
    }

    /**
     * 认证错误
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = InternalAuthenticationServiceException.class)
    public CommonResult internalAuthenticationServiceException(InternalAuthenticationServiceException e) {
        logger.error("error :{}", e.getMessage());
        return CommonResult.error(e.getMessage());
    }

}
